﻿

using hlog;
using hq;
using hutil;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 namespace hcache {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：PLDictCache                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/7/4 14:38:50                        
   *└────────────────────────────────────────────────┘
   **/
    public class StatusCache {
        public static int signalLife = 1000 * 5;
    
       
        static public Dictionary<string ,AbsStatus> dict = new Dictionary<string ,AbsStatus>();
        static public Dictionary<String ,BsonDocument> dictOld = new Dictionary<String ,BsonDocument>();
        static List<BsonDocument> defList = new List<BsonDocument>();
          public static T getSignalStatus<T>(int id) where T : Signal {
            lock( dict ) {
                foreach( var v in StatusCache.dict ) {
                    var value = v.Value;

                    if( value is SignalStatus<T> ) {
                        var status = ( SignalStatus<T> ) v.Value;
                        Dictionary<int ,T> signalDict = status.c;
                        if( signalDict.ContainsKey(id) ) {
                            T css = signalDict[id];
                            long des = DateTime.Now.Ticks - status.time.Ticks;
                            if( des > signalLife * 10000 ) {
                                LoggerCommon.consol($"{id}传输线信号超时{des / 10000 / 1000}秒" );
                                return null;
                            }
                            return css;
                        }
                    }
                }


            }
            
             return null;
        }
            static public List<BsonDocument> getDef() {
            lock( dictOld ) {
                List<BsonDocument> def = defList;
                defList = new List<BsonDocument>();
                return def;
            }
          
        }
          static public void  setStatus(Type t,AbsStatus status) {
            lock( dict ) {
                String id = t.Name;
                if( dict.ContainsKey(id) ) {
                    dict[id] = status;
                } else {
                    dict.Add(id ,status);
                }
            }
         }
         static public AbsStatus getStatus (Type t)   {
            String id = t.Name;
            return getStatus(id);
        }
        static public AbsStatus getStatus<T>(int index){
            String id = typeof(T).Name + index;
            return getStatus(id);
        }

        static public List<T> getStatus<T>() where T : AbsStatus {
            List<T> list = new List<T>();

            foreach( var kv in dict ) {
                if( kv.Value is T ) {
                    list.Add((T)kv.Value);


                }
            }
            return list;
        }
        static private  AbsStatus getStatus(String id)  {
   
             if( dict.ContainsKey(id) ) {
                AbsStatus absStatus = dict[id];
                long delay = DateTime.Now.Ticks - absStatus.getLastTime();
                delay /= 10000;
                if( delay > HUtil.ticksSecRate * 2 ) {
                    String s = delay + "";
                    int len = s.Length;
                     s = s.Substring(0 ,1) + "E" + (len - 1);
                    LoggerCommon.consol(string.Format("{0}statusTimeOut:{1}ms" ,id ,s));
                    return null;
                } else {
                    return absStatus;
                }
            }
            return null;
        }
      
         static public void  addDef(List<BsonDocument> def) {
            if( def.Count == 0 ) {
                return;
            }
            lock( dictOld ) {
                defList.AddRange(def);
            }
        }
          static public BsonDocument getHis( Type t ) {
            String id = t.Name;
             if( dictOld.ContainsKey(id) ) {
                 return dictOld[id];
            } else {
                 return null;
            }
         }
         static public void setHis(Type t ,BsonDocument b) {
            lock( dictOld ) {
                String id = t.Name;
                if( dictOld.ContainsKey(id) ) {
                    dictOld[id] = b;
                } else {
                    dictOld.Add(id ,b);
                }
            }
           
         }
      }
}
